package ConveyorBelt.State;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import ConveyorBelt.Util.ScoreRecord;
import ConveyorBelt.Util.ScoreTableLoader;

import ConveyorBelt.Base.Score;
import ConveyorBelt.Util.FontUtil;
import ConveyorBelt.Util.ImageUtil;

public class LevelLose extends BasicGameState {
	public static int stateID = 6;
	private int loseX, loseY;
	private int	exitGameButtonX, exitGameButtonY;
	private int backButtonX, backButtonY;
	private int mouseX, mouseY;
    private FadeOutTransition fot;
    private FadeInTransition fit;
	private float exitGameScale = 1;
	private float backScale = 1;
	private float scaleStep = 0.0001f;
	private String playerName;
	private boolean enterPressed = false;
	
	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		ImageUtil.loadAllImage();
		FontUtil.loadGameFont();
		playerName = new String();
		loseX = gc.getWidth()/2 - ImageUtil.lose.getWidth()/2;
		loseY = gc.getHeight()/2 - ImageUtil.lose.getHeight()/2 - 200;		
		exitGameButtonX = loseX + ImageUtil.lose.getWidth() - ImageUtil.exitGameButton.getWidth() + 200;
		exitGameButtonY = loseY + ImageUtil.lose.getHeight() + 300;
		backButtonX = loseX - 220;
		backButtonY = loseY + ImageUtil.lose.getHeight() + 300;
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		ImageUtil.background.draw(0, 0);
		ImageUtil.lose.draw(loseX, loseY);
		ImageUtil.exitGameButton.draw(exitGameButtonX, exitGameButtonY, exitGameScale);
		ImageUtil.backButton.draw(backButtonX, backButtonY, backScale);
		g.setFont(FontUtil.uf);
		g.drawString("Your score:     "+Score.getCurrentScore(),loseX + 20, loseY+300);
		g.drawString("Please enter your name:  ", loseX - 100, loseY + 350);
		g.drawString(playerName, loseX + 180, loseY + 350);
		g.drawString("Press Enter to save:  ", loseX - 100, loseY + 400);
		if(enterPressed) g.drawString("Saved to Score Table!", loseX - 100, loseY + 450); 
 		g.drawString("OOP 2D Game Project:      Can You Help Me???      - Author: Phu - Manh - Quang", 2200, 6200);
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException { 
		Input input = gc.getInput();
		mouseX = input.getMouseX();
		mouseY = input.getMouseY();
		boolean insideExitGame = false;
		boolean insideBack = false;
		int tmpX, tmpY;
		fot = new FadeOutTransition(Color.black);
    	fit = new FadeInTransition(Color.black);
		if((mouseX >= exitGameButtonX && mouseX <= exitGameButtonX + ImageUtil.exitGameButton.getWidth()) && 
				(mouseY >= exitGameButtonY && mouseY <= exitGameButtonY + ImageUtil.exitGameButton.getHeight())) {
			insideExitGame = true;
		}
		tmpY = loseY + ImageUtil.lose.getHeight() + 300;
		if(insideExitGame) {
			tmpX = loseX + ImageUtil.lose.getWidth() - ImageUtil.exitGameButton.getWidth() + 200 + 10;
			if(exitGameScale < 1.05f) exitGameScale += scaleStep*delta;
			if(exitGameButtonX < tmpX) exitGameButtonX ++;
			if(exitGameButtonY >= tmpY) exitGameButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
				//writeScore();
				gc.exit();
			}
		}
		else {
			tmpX = loseX + ImageUtil.lose.getWidth() - ImageUtil.exitGameButton.getWidth() + 200;
			if(exitGameScale > 1.0f) exitGameScale -= scaleStep*delta;
			if(exitGameButtonX > tmpX) exitGameButtonX--;
			if(exitGameButtonY <= tmpY) exitGameButtonY++;
		}
		
		if((mouseX >= backButtonX && mouseX <= backButtonX + ImageUtil.backButton.getWidth()) && 
				(mouseY >= backButtonY && mouseY <= backButtonY + ImageUtil.backButton.getHeight())) {
			insideBack = true;
		}
		tmpY = loseY + ImageUtil.lose.getHeight() + 300;
		if(insideBack) {
			tmpX = loseX - 200 + 10;
			if(backScale < 1.05f) backScale += scaleStep*delta;
			if(backButtonX < tmpX) backButtonX ++;
			if(backButtonY >= tmpY) backButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
				Score.setDefaultScore();
				Game.levelNumber = 1;
				sbg.enterState(MainMenu.stateID, fot, fit);
			}
		}
		else {
			tmpX = loseX - 200;
			if(backScale > 1.0f) backScale -= scaleStep*delta;
			if(backButtonX > tmpX) backButtonX--;
			if(backButtonY <= tmpY) backButtonY++;
		}
	}

	public void keyReleased(int key, char c) {
		if (key == Input.KEY_ENTER) {
		    writeScore();
		    enterPressed = true;
		}
		if (key == Input.KEY_BACK) {
		    if (playerName.length() > 0) {
		    	playerName = playerName.substring(0, playerName.length() - 1);
		    }
		}
		if (playerName.length() < 20) {
			if (key == Input.KEY_A) playerName = playerName + "a";
		    if (key == Input.KEY_B) playerName = playerName + "b";
		    if (key == Input.KEY_C) playerName = playerName + "c";
		    if (key == Input.KEY_D) playerName = playerName + "d";
		    if (key == Input.KEY_E) playerName = playerName + "e";
		    if (key == Input.KEY_F) playerName = playerName + "f";
		    if (key == Input.KEY_G) playerName = playerName + "g";
		    if (key == Input.KEY_H) playerName = playerName + "h";
		    if (key == Input.KEY_I) playerName = playerName + "i";
		    if (key == Input.KEY_J) playerName = playerName + "j";
		    if (key == Input.KEY_K) playerName = playerName + "k";
		    if (key == Input.KEY_L) playerName = playerName + "l";
		    if (key == Input.KEY_M) playerName = playerName + "m";
		    if (key == Input.KEY_N) playerName = playerName + "n";
		    if (key == Input.KEY_O) playerName = playerName + "o";
		    if (key == Input.KEY_P) playerName = playerName + "p";
		    if (key == Input.KEY_Q) playerName = playerName + "q";
		    if (key == Input.KEY_R) playerName = playerName + "r";
		    if (key == Input.KEY_S) playerName = playerName + "s";
		    if (key == Input.KEY_T) playerName = playerName + "t";
		    if (key == Input.KEY_U) playerName = playerName + "u";
		    if (key == Input.KEY_V) playerName = playerName + "v";
		    if (key == Input.KEY_W) playerName = playerName + "w";
		    if (key == Input.KEY_X) playerName = playerName + "x";
		    if (key == Input.KEY_Y) playerName = playerName + "y";
		    if (key == Input.KEY_Z) playerName = playerName + "z";
		    if (key == Input.KEY_SPACE) playerName = playerName + " ";
		}
		
    }

    private void writeScore() {
		try {
			ScoreTableLoader stl = new ScoreTableLoader("resources/score/scores.properties");
		    ArrayList<ScoreRecord> scores = stl.loadScoreTable(stl.filePath);
		    
		    for(int i=0; i<scores.size(); i++) {
	 			System.out.println(scores.get(i).getName()+"="+scores.get(i).getScore());
	 		
	 		}
		    
		    ScoreRecord sr = new ScoreRecord(playerName, Score.getCurrentScore());
		    scores.add(sr);
		    stl.saveScoreTable(scores);
		    for(int i=0; i<scores.size(); i++) {
	 			System.out.println(scores.get(i).getName()+"="+scores.get(i).getScore());
	 		
	 		}
		    //stl = null;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
    }
	
	@Override
	public int getID() {
		return stateID;
	}

}
